package com.ruoyi.vplat.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.vplat.dao.VplatDeviceDeptDao;
import com.ruoyi.vplat.domain.VplatDevice;
import com.ruoyi.vplat.domain.VplatDeviceDept;
import com.ruoyi.vplat.mapper.VplatDeviceMapper;
import com.ruoyi.vplat.service.IVplatDeviceService;

/**
 * 设备Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-29
 */
@Service
public class VplatDeviceServiceImpl implements IVplatDeviceService {
	@Autowired
	private VplatDeviceMapper vplatDeviceMapper;
	@Autowired
	private VplatDeviceDeptDao vplatDeviceDeptDao;

	/**
	 * 查询设备
	 * 
	 * @param id 设备主键
	 * @return 设备
	 */
	@Override
	public VplatDevice selectById(String id) {
		return vplatDeviceMapper.selectById(id);
	}

	/**
	 * 查询设备列表
	 * 
	 * @param vplatDevice 设备
	 * @return 设备
	 * @throws Exception
	 */
	@Override
	public List<Map<String, Object>> queryList(Map<String, Object> param) throws Exception {
		List<Map<String, Object>> list = vplatDeviceMapper.queryList(param);
		// BeanUtils.caseFormat(list); 在Mybatis拦截器插件中统一处理
		return list;
	}

	/**
	 * 查询设备列表
	 * 
	 * @param vplatDevice 设备
	 * @return 设备
	 */
	@Override
	public List<VplatDevice> selectList(VplatDevice vplatDevice) {
		return vplatDeviceMapper.selectList(vplatDevice);
	}

	/**
	 * 新增设备
	 * 
	 * @param vplatDevice 设备
	 * @return 结果
	 */
	@Override
	public int insert(VplatDevice vplatDevice) {
		if (StringUtils.isBlank(vplatDevice.getId())) {
			String id = BeanUtils.getSeqId("4415020000132", "vplat_device");
			vplatDevice.setId(id);
		}
		vplatDevice.setStatus("0");
		vplatDevice.setCreateTime(DateUtils.getNowDate());
		return vplatDeviceMapper.insert(vplatDevice);
	}

	/**
	 * 修改设备
	 * 
	 * @param vplatDevice 设备
	 * @return 结果
	 */
	@Override
	public int update(VplatDevice vplatDevice) {
		vplatDevice.setUpdateTime(DateUtils.getNowDate());
		return vplatDeviceMapper.update(vplatDevice);
	}

	/**
	 * 批量删除设备
	 * 
	 * @param ids 需要删除的设备主键
	 * @return 结果
	 */
	@Override
	public int deleteByIds(String[] ids) {
		return vplatDeviceMapper.deleteByIds(ids);
	}

	/**
	 * 删除设备信息
	 * 
	 * @param id 设备主键
	 * @return 结果
	 */
	@Override
	public int deleteById(String id) {
		return vplatDeviceMapper.deleteById(id);
	}

	@Override
	@Transactional
	public int bindDept(JSONObject params) {
		Integer deptId = params.getInteger("deptId");
		JSONArray devices = params.getJSONArray("devices");
		int result = 0;
		if (devices != null && devices.size() > 0) {
			for (int i = 0; i < devices.size(); i++) {
				String deviceId = devices.getString(i);
				VplatDeviceDept example = VplatDeviceDept.builder().deviceId(deviceId).deptId(deptId).build();
				List<VplatDeviceDept> list = vplatDeviceDeptDao.selectList(example);
				if (list == null || list.size() == 0) {
					example.setStatus("0");
					result += vplatDeviceDeptDao.insert(example);
				}
			}
		}
		return result;
	}

	@Override
	@Transactional
	public int unBindDept(JSONObject params) {
		Integer deptId = params.getInteger("deptId");
		JSONArray devices = params.getJSONArray("devices");
		int result = 0;
		if (devices != null && devices.size() > 0) {
			for (int i = 0; i < devices.size(); i++) {
				String deviceId = devices.getString(i);
				result += vplatDeviceDeptDao.deleteById(new HashMap<String, Object>(){{
					put("deptId", deptId);
					put("deviceId", deviceId);
				}});
			}
		}
		return result;
	}
}
